package com.hskn.hss.module.tlcarmanufacturer.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcarmanufacturer.entity.TlCarManufacturer;
import com.hskn.hss.module.tlcarmanufacturer.mapper.TlCarManufacturerMapper;
import com.hskn.hss.module.tlcarmanufacturer.service.ITlCarManufacturerService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Service
public class TlCarManufacturerServiceImpl extends ServiceImpl<TlCarManufacturerMapper, TlCarManufacturer> implements ITlCarManufacturerService {

    @Resource
    TlCarManufacturerMapper tlCarManufacturerMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlCarManufacturer tlCarManufacturer = params.getObject("data", TlCarManufacturer.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlCarManufacturer.setCreateby(uid);
        tlCarManufacturer.setCreatetime(new Date());
        tlCarManufacturer.setDeptid(deptid);
        if (tlCarManufacturer.getStatus() == null || tlCarManufacturer.getStatus().isEmpty()) {
            tlCarManufacturer.setStatus("1");
        }
        tlCarManufacturerMapper.insert(tlCarManufacturer);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlCarManufacturer tlCarManufacturer = params.getObject("data", TlCarManufacturer.class);
        tlCarManufacturer.setUpdateby(uid);
        tlCarManufacturer.setUpdatetime(new Date());
        tlCarManufacturerMapper.updateById(tlCarManufacturer);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlCarManufacturerMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlCarManufacturer getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlCarManufacturer tlCarManufacturer = tlCarManufacturerMapper.selectById(id);
        return tlCarManufacturer;
    }

    @Override
    public List getSelectList() {
        List resultList = tlCarManufacturerMapper.getSelectList();
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("dicname", params.getString("dicname"));
        map.put("status", params.getString("status"));
        Page<TlCarManufacturer> listPage = new Page<>(page, size, true);
        IPage<TlCarManufacturer> iPage = tlCarManufacturerMapper.listTlCarManufacturer(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("positionlist", iPage.getRecords());
        return json;
    }
    @Override
    public Map<String,TlCarManufacturer> getCarManufacturerMap(Wrapper<TlCarManufacturer> wrapper){
        Map<String,TlCarManufacturer> carManufacturerMap = new HashMap<>();
        List<TlCarManufacturer> carManufacturerList = tlCarManufacturerMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(carManufacturerList)){
            carManufacturerMap = GroupingByUtils.getGroupingForMapEntity(carManufacturerList,TlCarManufacturer::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return carManufacturerMap;
    }
    @Override
    public List<TlCarManufacturer> selectList(Wrapper<TlCarManufacturer> wrapper){
        List<TlCarManufacturer> carManufacturerList = tlCarManufacturerMapper.selectList(wrapper);
        return carManufacturerList;
    }
    @Override
    public TlCarManufacturer selectOne(String id){
        QueryWrapper<TlCarManufacturer> carManufacturerQueryWrapper = new QueryWrapper<TlCarManufacturer>();
        carManufacturerQueryWrapper.lambda().eq(TlCarManufacturer::getId,id);
        TlCarManufacturer carManufacturer = tlCarManufacturerMapper.selectOne(carManufacturerQueryWrapper);
        return carManufacturer;
    }
}
